

let BaseView = require('../BaseView');


class BaseAnimView extends BaseView{
    constructor(templateDiv) {
        super();

        this.rootEl = document.createElement('div');
        this.rootEl.innerHTML = templateDiv;
        this.lbInfo = this.rootEl.querySelector('.info');

        // 播放
        let btnPlay = this.rootEl.querySelector('.btnPlay');
        btnPlay.addEventListener('click',()=>{
            // TODO:是否需要判断正在动画??? 不需要,点击后 重新创建新的动画取代之前的
            //
            let selectedSp = editor.getInspectView().getSelectedSp();
            if(selectedSp){
                this._playAnim(selectedSp);

                // 动画的时候 隐藏
                editor.gameApp.hideSelectedFlag();
            }
        },false);

        // 停止
        let btnStop = this.rootEl.querySelector('.btnStop');
        btnStop.addEventListener('click',()=>{
            let selectedSp = editor.getInspectView().getSelectedSp();
            if(selectedSp){
                let actionMgr = cc.director.getActionManager();
                actionMgr.removeActionByTag(G.Tags.DecoBaseAction,selectedSp.node);
                editor.getInspAnimView().restoreInitData();
                editor.gameApp.selectDeco(selectedSp);
                this.updateInfoLabel();
            }
        },false);

        // 删除
        let btnDelete = this.rootEl.querySelector('.btnDelete');
        btnDelete.addEventListener('click',()=>{
            if(settings.showConfirmDialog('确认要删除动画么?')){
                // 动画的时候 隐藏
                let selectedSp = editor.getInspectView().getSelectedSp();
                if(selectedSp){
                    let inspectModel =  editor.getInspectView().getModel();
                    let actionMgr = cc.director.getActionManager();
                    actionMgr.removeActionByTag(G.Tags.DecoBaseAction,selectedSp.node);
                    editor.getInspAnimView().restoreInitData();

                    // 删除动画数据
                    selectedSp.anims = null;
                    inspectModel.anims = null;

                    editor.gameApp.selectDeco(selectedSp);
                }
                this.destroy();
            }
        },false);

        this.bindModel();
        this.reset();
    }


    // need override
    bindModel(){}
    reset(){}

    updateInfoLabel(){
        let selectSp = editor.getInspectView().getSelectedSp();
        if(selectSp){
            let actionMgr = cc.director.getActionManager();
            let action =  actionMgr.getActionByTag(G.Tags.DecoBaseAction,selectSp.node);
            if(action){
                this.lbInfo.textContent = '动画中.....';
                this.lbInfo.style.color = 'red';
                return;
            }
        }

        this.lbInfo.textContent = '停止';
        this.lbInfo.style.color = 'black';
    }
}

module.exports = BaseAnimView;
